/*


The MIT License

Copyright (c) 2007 Mohamed El-Shinnawy & Victor Martin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

/**
 * This file is part of Services.app.
 *
 * Services.app is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Services.app is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>
#import <UIKit/UITransitionView.h>
#import "ServiceTableCell.h"
#include "unistd.h"

@implementation ServiceTableCell

UITransitionView *_uitv;

- (ServiceTableCell *)initWithFrame:(CGRect)frame andValue:(BOOL)onOff {
  [super initWithFrame:frame];
  
  _control = [[UISwitchControl alloc] initWithFrame: CGRectMake(208.0f, 9.0f, 60.0f, 25.0f)];
  [_control setValue: onOff];
  [_control addTarget: self action:@selector(controlChanged:) forEvents:1<<6];
  [self addSubview: _control];
  [self setShowSelection: NO];
  
  _lastChange = [[NSDate date] retain];
  //_uitv = [[UITransitionView alloc] init];
  //[_uitv setDelegate: self];
  return self;
}

- (void)setEnabled:(BOOL)enable {
  [super setEnabled:enable];
  [_control setEnabled:enable];
}

- (void)setLoading:(BOOL)load {
  if ((load && _spinner != nil) || (!load && _spinner == nil)) {
    return;
  }
  if (_spinner == nil) {
    _spinner = [[UIProgressIndicator alloc] initWithFrame: CGRectMake(279.0f, 13.0f, 20.0f, 20.0f)];
    [_spinner setStyle: 1];
  }
  
  if (load) {
    [self _callDelegates:@selector(loadingWillBegin:)];
    [_spinner setAnimationDuration:1];
    [_spinner startAnimation];
    UITransitionView *_uitv = [[[UITransitionView alloc] init] autorelease];
    NSLog(@"Moving to spinner");
    [_uitv transition:6 fromView:_control toView: _spinner];
    [self addSubview: _spinner];
    [_spinner release]; 
  } else {
    /* To prevent the control from sometimes dissappearing forever due to timing issues
       we simply make a copy of it and put it into the next transtion */
    UISwitchControl *tmp;
    UISwitchControl *_newcontrol = [[UISwitchControl alloc] initWithFrame: CGRectMake(208.0f, 9.0f, 60.0f, 25.0f)];
    [_newcontrol setEnabled:[_control isEnabled]];
    [_newcontrol setValue: [_control value]];
    [_newcontrol addTarget: self action:@selector(controlChanged:) forEvents:1<<6];
    tmp = _control;
    _control = _newcontrol;
    [tmp release];

    UITransitionView *_uitv = [[[UITransitionView alloc] init] autorelease];
    NSLog(@"Moving to control");
    [_uitv transition:6 fromView: _spinner toView: _control];
    [self addSubview: _control];
    //[_spinner release]; 
    _spinner = nil;
    [self _callDelegates:@selector(loadingDidFinish:)];
  }
}

- (void)_callDelegates:(SEL)aSelector {
  NSEnumerator *iter = [_delegates objectEnumerator];
  id delegate;
  while((delegate = [iter nextObject])) {
    if ([delegate respondsToSelector:aSelector]) {
      [delegate performSelector:aSelector withObject:self];
    }
  }
}

- (BOOL)loading {
  return _spinner != nil;
}

- (BOOL)controlValue {
  return [_control value];
}

- (BOOL)setControlValue:(BOOL)state {
  [_control setValue:state];
  if ([self loading]) {
    [self setLoading:NO];
  } 
}

- (void)setControlChangedTarget:(id)target withAction:(SEL)action withUserInfo:(id)userInfo {
  [_realTarget release];
  [_userInfo release];
  
  _realTarget = target;
  _selector = action;
  _userInfo = userInfo;
  
  [_realTarget retain];
  [_userInfo retain];
}

- (void)controlChanged:(UISwitchControl *)control {
  if ([_lastChange timeIntervalSinceNow] > -0.3) {
    return;
  }

  [_lastChange release];
  _lastChange = [[NSDate date] retain];
  
  if ([self loading]) {
    [self setLoading:NO];
  } else {
    [self setLoading:YES];
  }
  
  NSLog(@"Sending a button click");
  [_realTarget performSelector:_selector withObject:self];
}

- (void)dealloc {
  [_delegates release];
  [_realTarget release];
  [_userInfo release];
  [_spinner release];
  [_control release];
  [_lastChange release];
  //[_uitv release];
  [super dealloc];
}

- (void)setDelegate:(id)delegate {
  if (_delegates == nil) {
    _delegates = [[NSMutableArray alloc] init];
  }
  
  [_delegates addObject:delegate];
}

@end;